package org.springblade.common.config;


import org.springblade.common.constant.LndxKTConstants;
import org.springblade.common.filter.PreviewFilter;
import org.springblade.core.secure.registry.SecureRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Blade配置
 *
 * @author Chill
 */
@Configuration
public class BladeConfiguration implements WebMvcConfigurer {

    /**
     * global public resource exclusion config
     *
     * @return
     */
    @Bean
    public SecureRegistry secureRegistry() {
        SecureRegistry secureRegistry = new SecureRegistry();
        secureRegistry.setEnabled(true);
        //过滤不检测token的接口 未登录也可以
        secureRegistry.excludePathPatterns("/blade-auth/**");
        secureRegistry.excludePathPatterns("/blade-log/**");
        secureRegistry.excludePathPatterns("/blade-system/menu/routes");
        secureRegistry.excludePathPatterns("/blade-user/geUserAgeTypeByAreaInfo");
        secureRegistry.excludePathPatterns("/blade-system/dict/**");
        secureRegistry.excludePathPatterns("/blade-develop/**");
        secureRegistry.excludePathPatterns("/blade-system/menu/auth-routes");
        secureRegistry.excludePathPatterns("/blade-system/menu/top-menu");
        secureRegistry.excludePathPatterns("/blade-system/dept/getDeptPageByAreaInfo");
        secureRegistry.excludePathPatterns("/blade-system/tenant/info");
        secureRegistry.excludePathPatterns("/blade-system/region/**");
        secureRegistry.excludePathPatterns("/blade-flow/process/resource-view");
        secureRegistry.excludePathPatterns("/blade-flow/process/diagram-view");
        secureRegistry.excludePathPatterns("/blade-flow/manager/check-upload");
        secureRegistry.excludePathPatterns("/doc.html");
        secureRegistry.excludePathPatterns("/js/**");
        secureRegistry.excludePathPatterns("/webjars/**");
        secureRegistry.excludePathPatterns("/swagger-resources/**");
        secureRegistry.excludePathPatterns("/druid/**");

///////////////TODO 第三方不授权回调通知 纳入白名单校验
//////////////////////自定义接口开发
        secureRegistry.excludePathPatterns("/upload/**");
        secureRegistry.excludePathPatterns("/blade-app/**");


        return secureRegistry;
    }

    @Bean
    @ConditionalOnProperty(value = "blade.preview.enabled", havingValue = "true")
    public PreviewFilter previewFilter() {
        return new PreviewFilter();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //FIXME 1.禁止全局跨域和全路径跨域
        //FIXME 2.跨域白名单配置
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true)
                .maxAge(36000000000L);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //registry.addResourceHandler("/upload/**").addResourceLocations("file:E:/home/");
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+ LndxKTConstants.FILE_PATH_RT);
    }

}
